package com.dsr.service.impl;

import cn.hutool.core.date.DateTime;
import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.consts.e.BaseProperty;
import com.dsr.consts.e.MangerProperty;
import com.dsr.dao.ManagerDao;
import com.dsr.entity.Manager;
import com.dsr.service.ManagerService;
import com.dsr.util.BasicUtil;
import com.dsr.util.IdWorker;
import com.dsr.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Manager)表服务实现类
 *
 * @author dsr
 * @since 2020-08-25
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerDao managerDao;
    @Autowired
    private IdWorker idWorker;
    @Override
    public Result getEntity(String id) {
        Manager entity = managerDao.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    @Override
    public Result save(Manager manager) {
        manager.setId(String.valueOf(idWorker.nextId()));
        manager.setDel(0);
        manager.setPassword(BasicUtil.getMD5(manager.getPassword()));
        managerDao.insertSelective(manager);
        return Result.success("保存成功");
    }

    @Override
    public Result update(Manager manager) {
        if(StringUtils.isNotEmpty(manager.getPassword())){
            manager.setPassword(BasicUtil.getMD5(manager.getPassword()));
        }
        managerDao.updateByPrimaryKeySelective(manager);
        return Result.success("修改成功");

    }

    @Override
    public Result delete(String id) {
        if (StringUtils.isNotEmpty(id)) {
            List<String> ids = Arrays.asList(id.split(","));
            if(ids.contains("1")){
                return Result.error("不能删除超级管理员");
            }
            Example example = new Example(Manager.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            managerDao.deleteByExample(example);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }

    @Override
    public Result query(int page, int size, Manager manager) {
        PageHelper.startPage(page, size);
        List<Manager> entityList = managerDao.select(manager);
        PageInfo<Manager> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<Manager>(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result list() {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del", 0);
        List<Manager> managers = managerDao.selectByExample(example);
        return Result.success(managers);
    }


    @Override
    public Result login(String userName, String password) {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName",userName);
        Manager manager = managerDao.selectOneByExample(example);
        String hex = DigestUtils.md5DigestAsHex(password.getBytes());
        if(manager==null)
            return Result.error(HttpStatus.NOT_FOUND.value(), "用户不存在");
        else if (!manager.getPassword().equals(BasicUtil.getMD5(password))){
            return Result.error(HttpStatus.NOT_FOUND.value(),"密码错误");
        }
        Map<String,String> map =new HashMap<>();
        map.put(MangerProperty.MANGER_ID,manager.getId());
        map.put(MangerProperty.REALER_NAME,manager.getRealName());
        map.put(BaseProperty.ROLE,MangerProperty.MANAGER);
        map.put(BaseProperty.ROLE_ID,manager.getRoleId());
        BasicUtil.setCookie("token",JwtUtil.getToken(map));
        manager.setLoginDate(DateTime.now());
        managerDao.updateByPrimaryKeySelective(manager);
        return Result.success("登录成功");
    }
}